from kafka.admin import KafkaAdminClient, NewTopic


def create_topic(topic_name, num_partitions=1, replication_factor=1):
    # Kafka 服务器配置
    admin_client = KafkaAdminClient(bootstrap_servers='10.10.1.80:9092')

    # 创建新主题
    topic_list = [NewTopic(name=topic_name, num_partitions=num_partitions, replication_factor=replication_factor)]
    try:
        admin_client.create_topics(new_topics=topic_list, validate_only=False)
        print(f"Topic '{topic_name}' created successfully.")
    except Exception as e:
        print(f"Failed to create topic: {e}")
    finally:
        admin_client.close()


if __name__ == "__main__":
    topic_name = "DRS00001"
    create_topic(topic_name)
